package com.example.demo.controller;

import com.example.demo.common.Result;
import com.example.demo.dto.TouristListDTO;
import com.example.demo.entity.Attraction;
import com.example.demo.entity.Video;
import com.example.demo.entity.RecordCount;
import com.example.demo.service.AttractionRecordService;
import com.example.demo.service.AttractionService;
import com.example.demo.service.VideoService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @program: demo
 * @description:
 * @author: Lelouch
 * @create: 2024-09-04 15:41
 **/
@CrossOrigin
@RestController
@RequestMapping("/attraction")
public class AttractionController {
    @Resource
    private AttractionService attractionService;
    @Resource
    private AttractionRecordService attractionRecordService;
    @Resource
    private VideoService videoService;

    @GetMapping("/list")
    public Result list(){
        PageInfo<Attraction> attractionList = attractionService.list();
        if(attractionList != null){
            return Result.success(attractionList);
        }
        else {
            return Result.error("景点列表为空!");
        }
    }

    @GetMapping("/listByParams")
    public Result listByParams(@RequestParam Integer currentPage, @RequestParam Integer pageSize){
        PageInfo<Attraction> attractionList = attractionService.listByParams(currentPage,pageSize);
        if(attractionList != null){
            return Result.success(attractionList);
        }
        else {
            return Result.error("景点列表为空!");
        }
    }

    @GetMapping("/num")
    public Result num(){
        return Result.success(attractionService.list().getSize());
    }

    @PostMapping("/addAttraction")
    public Result addAttraction(@RequestBody Attraction attraction) {
        if(attractionService.addAttraction(attraction) != 1){
            return Result.error("景点添加失败");
        }
        return Result.success();
    }

    @PostMapping("/changeAttraction")
    public Result changeAttraction(@RequestBody Attraction attraction){
        if(attractionService.changeAttraction(attraction)){
            return Result.success();
        }
        else {
            return Result.error("更新失败!");
        }
    }

    @GetMapping("")
    public Result findAttractionById(@RequestParam Integer id){
        Attraction attraction = attractionService.getAttractionById(id);
        if(attraction != null){
            return Result.success(attraction);
        }
        else return Result.error("未查询到景点信息!");
    }

    @DeleteMapping("/deleteById")
    public Result deleteAttractionById(@RequestParam Integer id){
        if(attractionService.deleteAttractionById(id)){
            return Result.success();
        }
        else return Result.error("删除失败!");
    }

    @GetMapping("/touristExist")
    public Result touristExist(@RequestParam Integer attractionId){
        Integer ret = attractionRecordService.touristExist(attractionId);
        if(ret >= 0){
            return Result.success(ret);
        }
        return Result.error("获取失败!");
    }

    @PostMapping("/touristList")
    public Result touristList(@RequestBody TouristListDTO tlDTO){
        PageInfo<RecordCount> recordCountList = attractionService.touristList(tlDTO);
        if(recordCountList != null){
            return Result.success(recordCountList);
        }
        else {
            return Result.error("出入记录列表为空!");
        }
    }

    @GetMapping("/video")
    public Result videoList(@RequestParam Integer attractionId){
        List<Video> videoList = videoService.selectByAttractionId(attractionId);
        if(videoList != null){
            return Result.success(videoList);
        }
        return Result.error("查询视频信息为空!");
    }
}
